package storm;

import kafka.javaapi.consumer.ConsumerConnector;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

import java.util.*;

/**
 * @author Hanhan_Jun
 * @date 2021年12月08日 15:08
 */
public class Spout2 extends BaseRichSpout {
    private SpoutOutputCollector collector = null;
    private ConsumerConnector consumer;

    @Override
    public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
        this.collector = spoutOutputCollector;
    }

    @Override
    public void nextTuple() {
        Properties prop=new Properties();
        prop.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.30.141:9092,192.168.30.142:9092,192.168.30.143:9092");
        prop.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        prop.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);
        prop.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG,"30000");
        prop.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        prop.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"1000");
        //latest收最新的数据 none会报错 earliest最早的数据
        prop.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"latest");
        prop.put(ConsumerConfig.GROUP_ID_CONFIG,"G2");
        //创建消费者
        KafkaConsumer<byte[], byte[]> consumer = new KafkaConsumer<>(prop);
        consumer.subscribe(Collections.singleton("orderdetail"));//订阅

        while (true){
            ConsumerRecords<byte[], byte[]> poll = consumer.poll(100);
            Iterator<ConsumerRecord<byte[], byte[]>> iterator = poll.iterator();
            while (iterator.hasNext()) {
                ConsumerRecord<byte[], byte[]> next = iterator.next();
                String tuple = String.valueOf(next.value());
                String[] orderdetail = tuple.split(",");
                String orderdetail_id = orderdetail[0].replace("{","").split(":")[2].replace("\"","");
                String orderid = orderdetail[1].split(":")[1].replace("\"","");
                String sender = orderdetail[2].split(":")[1].replace("\"","");
                String sendplace = orderdetail[3].split(":")[1].replace("\"","");
                String recipient = orderdetail[4].split(":")[1].replace("\"","");
                String receiveplace = orderdetail[5].split(":")[1].replace("\"","").substring(0,2);
                String methodofpayment = orderdetail[6].split(":")[1].replace("\"","");
                String paytime = orderdetail[7].substring(22,24).replace("\"","");//11-29
                Double freight = new Double(orderdetail[8].split(":")[1].replace("\"",""));
                Double total = new Double(orderdetail[9].split(":")[1].replace("\"","").replace("}","").replace("]",""));
                collector.emit(new Values(orderdetail_id,orderid,sender,sendplace,recipient,receiveplace,methodofpayment,paytime,freight,total));
            }
        }

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields
                (new String[]{"orderdetail_id","orderid","sender","sendplace","recipient","receiveplace","methodofpayment","paytime","freight","total"}));
    }
}
